<?php
session_start();

include_once("../../Misc.php");
include_once("../../helpers/CHtml.php");
include_once("../../helpers/CJavascript.php");
include_once("../../helpers/Composer.php");
include_once("../../classCol/classOracleDb.php");
include_once("../../models/GlobalFetchers.php");
include_once("../../models/GlobalHandlers.php");

include_once("../controllers/local.php");
include_once("../models/fetchers.php");
include_once("../models/handlers.php");

$type       = _CASH_BANK_OUT;
$tx         = _CASH_BANK_CCL_DP_NAME;

$GHtml      = new CGHtml;
$GJs        = new CGJavascript;
$Misc       = new Misc;

$CLocal     = new CLocal;
$CLHandlers = new CLHandlers;
$CLFetchers = new CLFetchers;
$CFetchers  = new GlobalFetchers;
$CHandlers  = new GlobalHandlers;

$SourceDb   = new Composer;
$SourceDb   = $SourceDb->init("db");

$SourceDb->add("srcDbMutDp",            "MUT_DP_SUPPLIER");
$SourceDb->add("srcDbSourceHdr",        "PCH_HDR");
$SourceDb->add("srcDbTmpDtlCclDp",      "C_B_CCL_DP_SUPP_DTL_TMP");
$SourceDb->add("srcDbTmpSubCclDp",      "C_B_CCL_DP_SUPP_SUB_DTL_TMP");
$SourceDb->add("srcDbDtlCclDp",         "C_B_CCL_DP_SUPP_DTL");
$SourceDb->add("srcDbSubCclDp",         "C_B_CCL_DP_SUPP_SUB_DTL");
$SourceDb->add("srcDbMstCb",            "MST_CASH_BANK");
$SourceDb->add("srcDbMutDp",            "MUT_DP_SUPPLIER");

$srcDbMstCb = "MST_CASH_BANK";

$curPeriode = " TO_DATE('".$_SESSION["sysPeriode"]."','mm-YYYY') ";

echo  $GHtml->title("DP Out");

$Db = new oracleDb;
$Db->init($_SESSION["ORCL"],
          $_SESSION["OPASS"],
          $_SESSION["SID"],
          _NO_COMMENT);

if($Db->connect()){


}else{

  $Misc->usrDieJs(_DB_CONN_ERROR);
}

echo  $GJs->includeThis(_TYPE_CSS,"/ecs/myMenu.css").
      $GJs->includeThis(_TYPE_JS ,"/ecs/javascript/GMisc.js").
      $GJs->includeThis(_TYPE_JS ,"/ecs/tocmenu_31/javascript/misc.js").
      $GJs->includeThis(_TYPE_JS ,"/ecs/tocmenu_31/javascript/ajax.js").
      $GJs->stopRKey();

/**
 * Handle the submit
 */
if($_POST["Save"]){

  require("../controllers/procSave.php");
}else
if($_POST["Add"]){

  require("../controllers/procAdd.php");
}else
if($_POST["Drop"]){

  require("../controllers/procDrop.php");
}else{

  /**
   * make sure we are clean
   */
  unset($_SESSION["init_sub"]);

  /**
   * Fetch previous entried
   * if any
   */
  $CLFetchers->fetchDtlDp($Db,
                          $_SESSION["tmpJid"],
                          $SourceDb->fetchValue("srcDbTmpDtlCclDp"),
                          $nDetail,
                          $lstDetail);

  if($nDetail > 0){

    $_POST["totInvoice"]  = $lstDetail["TOT_INVOICE"][0];
    $_POST["totPayment"]  = $lstDetail["PAYMENT"][0];
    $_POST["gainEnt"]     = $lstDetail["GAIN_LOSS"][0];
    $_POST["discEnt"]     = $lstDetail["DISCOUNT"][0];

    $CLFetchers->fetchSubDp($Db,
                            $_SESSION["tmpJid"],
                            $SourceDb->fetchValue("srcDbTmpSubCclDp"),
                            $nSubDetail,
                            $lstSubDetail);

    for($iRow = 0; $iRow < $nSubDetail ; $iRow++){

      $_POST["notaSrcEnt"][$iRow]   = $lstSubDetail["REFERENCE"][$iRow];
      $_POST["cbSource"][$iRow]     = $lstSubDetail["SOURCE"][$iRow];
      $_POST["trDateEnt"][$iRow]    = date("d-m-Y",
                                           strtotime($lstSubDetail["TR_DATE"][$iRow]));
      $_POST["dueDateEnt"][$iRow]   = date("d-m-Y",
                                           strtotime($lstSubDetail["DUE_DATE"][$iRow]));
      $_POST["currencyEnt"][$iRow]  = $lstSubDetail["CURRENCY"][$iRow];
      $_POST["rateEnt"][$iRow]      = $lstSubDetail["RATE"][$iRow];
      $_POST["remainEnt"][$iRow]    = $lstSubDetail["REMAIN"][$iRow];
      $_POST["paymentEnt"][$iRow]   = $lstSubDetail["AMOUNT"][$iRow];
    }

    $_SESSION["init_sub"] = $nSubDetail;
  }else{

    /* Do nothing */
  }
}

echo  "<form  name='outputDp' action=# method=post>\n".
      "<table border='1' class='dedeq' align='center'>\n".
      "<tr>\n".
      "<th> No        </th>\n".
      "<th> Drop      </th>\n".
      "<th> Cash-Bank </th>\n".
      "<th> Date      </th>\n".
      "<th> Due       </th>\n".
      "<th> Whose     </th>\n".
      "<th> Currency  </th>\n".
      "<th> Rate      </th>\n".
      "<th> Remain    </th>\n".
      "<th> Amount    </th>\n".
      "</tr>\n";

for($iRow = 0; $iRow < $_SESSION["init_sub"]; $iRow++){

  $propAmount = $GJs->jsOnFocus("GMisc.setYellow2(this);
                                 GMisc.syncVal2F(\"paymentEnt[".$iRow."]\",
                                                 \"totInvoice\");").

                $GJs->jsOnBlur ("GMisc.setWhite2(this);
                                 GMisc.sumVal2F(\"paymentEnt[".$iRow."]\",
                                                \"totInvoice\");
                                 Misc.computeAll(\"totInvoice\",
                                                 \"totPayment\",
                                                 \"gainEnt\",
                                                 \"discEnt\");");

  echo  "<tr>\n".

        "<td> ".($iRow + 1)." </td>\n".

        "<td> ".$GHtml->formCheckBox("dropBox[]",
                                     ($iRow + 1),
                                     $Misc->stdProperties())." </td>\n".

        "<td> ".$GHtml->formInput("notaSrcEnt[".$iRow."]",
                                  $_POST["notaSrcEnt"][$iRow],
                                  $Misc->stdProperties().
                                  _MODE_READONLY)." </td>\n".

        "<td> ".$GHtml->formInput("trDateEnt[".$iRow."]",
                                  $_POST["trDateEnt"][$iRow],
                                  $Misc->stdNumStyle(8).
                                  $Misc->stdProperties().
                                  _MODE_READONLY)." </td>\n".

        "<td> ".$GHtml->formInput("dueDateEnt[".$iRow."]",
                                  $_POST["dueDateEnt"][$iRow],
                                  $Misc->stdNumStyle(8).
                                  $Misc->stdProperties().
                                  _MODE_READONLY)." </td>\n".

        "<td> ".$GHtml->formInput("whoseEnt[".$iRow."]",
                                  $_GET["whoseName"],
                                  $Misc->stdNumStyle(12).
                                  $Misc->stdProperties().
                                  _MODE_READONLY)." </td>\n".

        "<td> ".$GHtml->formInput("currencyEnt[".$iRow."]",
                                  $_POST["currencyEnt"][$iRow],
                                  $Misc->stdNumStyle(5).
                                  $Misc->stdProperties().
                                  _MODE_READONLY)." </td>\n".

        "<td> ".$GHtml->formInput("rateEnt[".$iRow."]",
                                  $_POST["rateEnt"][$iRow],
                                  $Misc->stdNumStyle(5).
                                  $Misc->stdProperties().
                                  _MODE_READONLY)." </td>\n".

        "<td> ".$GHtml->formInput("remainEnt[".$iRow."]",
                                  $_POST["remainEnt"][$iRow],
                                  $Misc->stdNumStyle(12).
                                  $Misc->stdProperties().
                                  _MODE_READONLY)." </td>\n".

        "<td> ".$GHtml->formInput("paymentEnt[".$iRow."]",
                                  $_POST["paymentEnt"][$iRow],
                                  $Misc->stdNumStyle(12).
                                  $Misc->stdKeyProp("paymentEnt[".($iRow+1)."]").
                                  $propAmount)." </td>\n".

        "</tr>\n".

        $GHtml->formHidden("gainLossEnt[".$iRow."]",
                           $_POST["gainLossEnt"][$iRow]).

        $GHtml->formHidden("cbSource[".$iRow."]",
                           $_POST["cbSource"][$iRow]);

  /**
   * calculates
   */
  $gainOnRate   += ($_POST["paymentEnt"][$iRow] * $_POST["gainLossEnt"][$iRow]);

  $totOfInvoice += $_POST["paymentEnt"][$iRow];
}

/**
 * set the amount
 */
$_POST["gainEnt"]    = $gainOnRate;

$_POST["totInvoice"] = $totOfInvoice;

$_POST["discEnt"]    = ($_GET["totSrcNet"]   -
                        $_POST["totInvoice"] -
                        $_POST["gainEnt"]);

echo  "<tr>\n".

      "<td ".$GHtml->rowColSpan( 1, 8)." align='right'>  </td>\n".

      "<td> #Invoice </td>\n".

      "<td> ".$GHtml->formInput("totInvoice",
                                $_POST["totInvoice"],
                                //$Misc->stdKeyProp().
                                $Misc->stdNumStyle(12).
                                $Misc->stdProperties())." </td>\n".

      "</tr>\n".
      "<tr>\n".

      "<td ".$GHtml->rowColSpan( 1, 8)." align='right'>  </td>\n".

      "<td> #Cancel </td>\n".

      "<td> ".$GHtml->formInput("totPayment",
                                $_GET["totSrcNet"],
                                //$Misc->stdKeyProp().
                                $Misc->stdNumStyle(12).
                                $Misc->stdProperties().
                                _MODE_READONLY)." </td>\n".

      "</tr>\n".
      "<tr>\n".

      "<td ".$GHtml->rowColSpan( 1, 8)." align='right'>  </td>\n".

      "<td> #Gain On Rate </td>\n".

      "<td> ".$GHtml->formInput("gainEnt",
                                $_POST["gainEnt"],
                                //$Misc->stdKeyProp().
                                $Misc->stdNumStyle(12).
                                $Misc->stdProperties())." </td>\n".

      "</tr>\n".
                                "<tr>\n".

      "<td ".$GHtml->rowColSpan( 1, 8)." align='right'>  </td>\n".

      "<td> #Discount </td>\n".

      "<td> ".$GHtml->formInput("discEnt",
                                $_POST["discEnt"],
                                //$Misc->stdKeyProp().
                                $Misc->stdNumStyle(12).
                                $Misc->stdProperties())." </td>\n".

      "</tr>\n".

      "</table><br/>\n";

echo  "<center>\n".

      $GHtml->formInputButton( "Save",
                               "Save",
                               "Save",
                               $propSave).
      $GHtml->formInputButton( "Add",
                               "Add",
                               "Add",
                               $propAddTx).
      $GHtml->formInputButton( "Drop",
                               "Drop",
                               "Drop",
                               $propDrow).

      "</center>\n".
      "<br/>\n";

/**
 * List all issued DP
 */
$CLFetchers->fetchDpIssued($Db,
                           $_GET["whoseCode"],
                           $curPeriode,
                           $_POST["typeEnt"],
                           $SourceDb->fetchValue("srcDbMutDp"),
                           $nIssued,
                           $issued);

echo  "<table border='1' class='dedeq' align='center'>\n".
      "<tr>\n".
      "<th> No      </th>\n".
      "<th> Sel     </th>\n".
      "<th> Invoice </th>\n".
      "<th> Date    </th>\n".
      "<th> Due     </th>\n".
      "<th> Whose   </th>\n".
      "<th> Amount  </th>\n".
      "</tr>\n";

for($iRow = 0; $iRow < $nIssued ; $iRow++){

  $propInvoice = $GJs->jsOnChange("Misc.setInvoice(this,
                                                   \"prevStsInvoice[".$iRow."]\",
                                                   \"ivcReference[".$iRow."]\");");

  echo  "<tr>\n".

        "<td> ".($iRow+1)." </td>\n".

        "<td> ".$GHtml->formCheckBox("invoice[".$iRow."]",
                                     "",
                                     $propInvoice)." </td>\n".

        "<td> ".$GHtml->formInput("ivcReference[".$iRow."]",
                                  $issued["REFERENCE"][$iRow],
                                  $Misc->stdProperties().
                                  _MODE_READONLY)." </td>\n".

        "<td> ".date("d-m-Y",
                     strtotime($issued["TR_DATE"][$iRow]))." </td>\n".

        "<td> ".date("d-m-Y",
                     strtotime($issued["DUE_DATE"][$iRow]))." </td>\n".

        "<td> ".$_GET["whoseName"]." </td>\n".

        "<td align='right'>
              ".number_format($issued["AMOUNT"][$iRow],2,".",",")." </td>\n".

        "</tr>\n".

        $GHtml->formHidden("source[".$iRow."]",
                           $issued["SOURCE"][$iRow]);

        $GHtml->formHidden("prevStsInvoice[".$iRow."]",
                           $_POST["prevStsInvoice"][$iRow]);

}

echo  $GHtml->formHidden("typeEnt",
                         $type).

      $GHtml->formHidden("txEnt",
                         $tx).
      "</table><br/>\n";

echo  "<center>\n".

      $GHtml->formInputButton( "Save",
                               "Save",
                               "Save",
                               $propSave).
      $GHtml->formInputButton( "Add",
                               "Add",
                               "Add",
                               $propAddTx).
      $GHtml->formInputButton( "Drop",
                               "Drop",
                               "Drop",
                               $propDrow).

      "</center>\n".
      "<br/>
       </form>\n";

$Db->dc();
?>